package com.supdragon.app.wxapi

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import com.lfc.myframe.R
import com.lfc.myframe.event.EBParams
import com.lfc.myframe.share.Const
import com.lfc.myframe.utils.LgU
import com.mdchina.juhai.utils.ToastUtils
import com.mdchina.thelifeorder.share.MessageEvent
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import org.greenrobot.eventbus.EventBus


/*
   *@Author LFC
   *@Date 2020-1-15 15:36:02
   *@Remarks:WXPayEntryActivity
   */
class WXPayEntryActivity : Activity(), IWXAPIEventHandler {
//  private var StrValue = ""
    //  private var Type = 0

    companion object {
        fun EnterThis(context: Context, string: String = "", int: Int = 0) {
            var intent = Intent(context, WXPayEntryActivity().javaClass)
            intent.putExtra("StrID", string)
            intent.putExtra("Type", int)
            context.startActivity(intent)
        }
    }

    private var api: IWXAPI? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_wxentry)
        //title = intent!!.getStringExtra("StrID")
        // Type = intent!!.getIntExtra("Type", 0)
//        initMVP()
        initView()
        //initData()
        getData()
        api = WXAPIFactory.createWXAPI(this, Const.APPID)
        api?.handleIntent(intent, this)

    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        setIntent(intent)
        api?.handleIntent(intent, this)
    }

    private fun initView() {

    }

    private fun getData() {

    }

    override fun onResp(resp: BaseResp?) {
        Log.i("WXPAY", "onPayFinish, errCode = " + resp?.errCode)

        // errCode：0 成功， -2 取消支付， -1 错误
        // errCode：0 成功， -2 取消支付， -1 错误
        if (resp?.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp?.errCode == 0) { //                Intent intent = new Intent(baseContext, SuccessActivity.class);
//                intent.putExtra("price", price);
//                intent.putExtra("hint", getIntent().getStringExtra("hint_success"));
//                startActivity(intent);
                EventBus.getDefault().post(MessageEvent(EBParams.EB_PaySuccess, typeValue = 1))
                finish()
            } else if (resp?.errCode == -2) {
                ToastUtils.showToask(this, "您取消了支付！")
                onBackPressed()
            } else if (resp?.errCode == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM) {
                val launchMiniProResp =
                    resp as WXLaunchMiniProgram.Resp
                val extraData =
                    launchMiniProResp.extMsg //对应小程序组件 <button open-type="launchApp"> 中的 app-parameter 属性
                LgU.d("onResp:" + extraData.toString())

            } else {
                ToastUtils.showToask(this, "支付失败！")
                onBackPressed()
            }
        }

    }

    override fun onReq(p0: BaseReq?) {
    }
}
